//
//  WisCameraControlViewController.m
//  wAudioShare
//
//  Created by 赵帅 on 14-3-18.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "WisCameraControlViewController.h"
#import "WISInterfaces.h"
#import "CameraWebView.h"

#define WebViewPageDirectionCtrlButtonsHeight  48.0

@interface WisCameraControlViewController (){
    BOOL isV;
    BOOL isH;
    CALayer *vLayer;
}

@property (strong) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UIButton *hBtn;
@property (weak, nonatomic) IBOutlet UIButton *vBtn;
@property (nonatomic, strong) CameraWebView *cameraView;
@end

@implementation WisCameraControlViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    vLayer.frame =  CGRectMake((self.view.frame.size.width - 310)/2,(self.view.frame.size.width - 235)/2 , 310, 235);
    self.cameraView = [[[NSBundle mainBundle] loadNibNamed:@"CameraWebView" owner:self options:nil]lastObject];
    
    self.cameraView.camURL = self.camURL;
    NSLog(@"wisCamerVC viewDidLoad %@",self.camURL);
//    self.cameraView.frame = CGRectMake(0, 0, 320, 310);
    vLayer.contents = self.cameraView;
    self.cameraView.frame = CGRectMake((self.view.frame.size.width - 310)/2,(self.view.frame.size.width - 235)/2 , 310, 235);
    [self.view addSubview:self.cameraView];
    
    [self.hBtn addTarget:self action:@selector(horizontalBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.vBtn addTarget:self action:@selector(verticalBtn) forControlEvents:UIControlEventTouchUpInside];
    isV = NO;
    isH = NO;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];


    CGSize size = self.view.bounds.size;
    size.height = size.height - self.bottomView.bounds.size.height + WebViewPageDirectionCtrlButtonsHeight;
//    self.cameraView.frame = CGRectMake((self.cameraView.frame.size.width-310)/2, (self.cameraView.frame.size.height - 130)/2, 310, 235);
   NSLog(@"w:%f,h:%f",self.cameraView.frame.size.width,self.cameraView.frame.size.height);
    
    [self.cameraView loadRequest];
    
}


- (void)dealloc
{
//    [self.cameraView.dateTimer invalidate];
//    self.cameraView.dateTimer = nil;
}

#pragma mark - navigation methods
-(NSString *)navigationBarTitle
{
    return @"WIS Demo";
}

-(IBAction)cut:(id)sender
{
    [self showMHUD:@"正在保存..."];
    
    UIImage *image = [self captureScreen:self.cameraView];
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    [self hideMHUD:@"保存成功" success:YES];
}


-(UIImage*)captureScreen:(UIView*)viewToCapture{
    
    CGSize overallSize = viewToCapture.bounds.size;
    overallSize.height -= WebViewPageDirectionCtrlButtonsHeight;
    UIGraphicsBeginImageContext(viewToCapture.bounds.size);
    // Save the current bounds
    CGRect tmp = viewToCapture.bounds;
    viewToCapture.bounds = CGRectMake(0, 0, overallSize.width, overallSize.height);
    // Wait for the view to finish loading.
    // This is not very nice, but it should work. A better approach would be
    // to use a delegate, and run the capturing on the did finish load event.
    while ([self.cameraView isWebViewLoading]) {
        [NSThread sleepForTimeInterval:0.1];
    }
    [viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // Restore the bounds
    viewToCapture.bounds = tmp;
    return viewImage;
}

- (void)horizontalBtn
{
    if (!isH)
    {
        [self.cameraView.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
        [self.cameraView.layer setTransform:CATransform3DMakeRotation(0, 0, 1, 0)];
        [UIView animateWithDuration:0 delay:0.0f options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveLinear animations:^{
            [self.cameraView.layer setTransform:CATransform3DMakeRotation(M_PI, 0, 1, 0)];
        } completion:^(BOOL finished) {
            isH = YES;
        }];
    }
    else
    {
        [self.cameraView.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
        [self.cameraView.layer setTransform:CATransform3DMakeRotation(0, 0, 1, 0)];
        [UIView animateWithDuration:0 delay:0.0f options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveLinear animations:^{
            [self.cameraView.layer setTransform:CATransform3DMakeRotation(0, 0, 1, 0)];
        } completion:^(BOOL finished) {
            isH = NO;
        }];
    }
   
}

- (void)verticalBtn
{
    if (!isV) {
        [self.cameraView.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
        [self.cameraView.layer setTransform:CATransform3DMakeRotation(0, 1, 0, 0)];
        [UIView animateWithDuration:0 delay:0.0f options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveLinear animations:^{
            [self.cameraView.layer setTransform:CATransform3DMakeRotation(M_PI, 1, 0, 0)];
        } completion:^(BOOL finished) {
            isV = YES;
        }];
    }
    else
    {
        [self.cameraView.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
        [self.cameraView.layer setTransform:CATransform3DMakeRotation(0, 1, 0, 0)];
        [UIView animateWithDuration:0 delay:0.0f options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveLinear animations:^{
            [self.cameraView.layer setTransform:CATransform3DMakeRotation(0, 1, 0, 0)];
        } completion:^(BOOL finished) {
            isV = NO;
        }];
    }
}

@end
